package com.hmy.security.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmy.common.exception.CustomException;
import com.hmy.security.entity.*;
import com.hmy.security.exception.UserException;
import com.hmy.security.mapper.RolesUserMapper;
import com.hmy.security.service.*;
import com.hmy.security.vo.UserRolesVo;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户身份关联表 服务实现类
 * </p>
 *
 * @author hmy
 * @since 2023-01-30
 */
@Service
public class RolesUserServiceImpl extends ServiceImpl<RolesUserMapper, RolesUser> implements IRolesUserService {

    @Autowired
    private IUserService userService;
    @Autowired
    private IRolesService rolesService;
    @Autowired
    private IMenuService menuService;
    @Autowired
    private IMenuRolesService menuRolesService;
    @Autowired
    private IMenuUserService menuUserService;

    private UserRolesVo handleGetRoles(User user) {
        List<Integer> roleIds = this.lambdaQuery()
                .eq(RolesUser::getUid, user.getUid())
                .list().stream()
                .map(RolesUser::getRoleId)
                .collect(Collectors.toList());
        List<Integer> menuIds = menuRolesService.lambdaQuery()
                .in(MenuRoles::getRoleId, roleIds)
                .list().stream()
                .map(MenuRoles::getMenuId)
                .collect(Collectors.toList());
        menuUserService.lambdaQuery()
                .eq(MenuUser::getUid, user.getUid())
                .list()
                .forEach(e -> {
                    if (e.getIsAdd()) {
                        menuIds.add(e.getId());
                    } else {
                        menuIds.remove(e.getId());
                    }
                });
        UserRolesVo userRolesVo = new UserRolesVo();
        BeanUtils.copyProperties(user, userRolesVo);
        if (ObjectUtils.isNotEmpty(roleIds)) {
            userRolesVo.setRoles(rolesService.lambdaQuery().in(Roles::getId, roleIds).list());
        }
        if (ObjectUtils.isNotEmpty(menuIds)) {
            userRolesVo.setMenus(menuService.lambdaQuery().in(Menu::getId, menuIds).list());
        }
        return userRolesVo;
    }

    public UserRolesVo getRolesByUid(Long uid) {
        User user = userService.getUserByUid(uid);
        if (ObjectUtils.isEmpty(user)) {
            throw new CustomException(UserException.NOT_FOUND);
        }
        return handleGetRoles(user);
    }

    @Override
    public UserRolesVo getRolesByUsername(String name) {
        User user = userService.getUserByName(name);
        if (ObjectUtils.isEmpty(user)) {
            throw new CustomException(UserException.NOT_FOUND);
        }
        return handleGetRoles(user);
    }
}
